package com.jay.nio.channel.clazz;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;

public class PipeCls {
	public static void main(String[] args) throws IOException {
		// 创建一个管道
		Pipe pipe = Pipe.open();
		final Pipe.SinkChannel psic = pipe.sink();
		final Pipe.SourceChannel psoc = pipe.source();
		System.out.println(psic.validOps());
		System.out.println(psoc.validOps());

		Thread tPwriter = new Thread() {

			public void run() {
				try {
					// 创建一个线程，利用管道的写入口Pipe.SinkChannel类型的psic往管道里写入指定ByteBuffer的内容
					psic.write(ByteBuffer.wrap("Hello,Pipe!"
							.getBytes("utf-16BE")));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};

		Thread tPreader = new Thread() {
			public void run() {
				int bbufferSize = 1024 * 2;
				ByteBuffer bbuffer = ByteBuffer.allocate(bbufferSize);
				try {
					// 创建一个线程，利用管道的读入口Pipe.SourceChannel类型的psoc将管道里内容读到指定的ByteBuffer中
					psoc.read(bbuffer);
					bbuffer.flip();
					byte[] content = new byte[bbuffer.limit()];
					bbuffer.get(content);
					System.out.println("Content:"
							+ new String(content, "utf-16BE"));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};

		tPwriter.start();
		tPreader.start();

	}
}
